package com.mzkj.wanjulaile.mobile.api.modules.coupon.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mzkj.common.data.StatusCode;
import com.mzkj.common.utils.Collections3;
import com.mzkj.common.utils.StringUtils;
import com.mzkj.wanjulaile.mobile.api.common.controller.BaseController;
import com.mzkj.wanjulaile.mobile.api.common.entity.ResultEntity;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.Account;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.VipScheme;
import com.mzkj.wanjulaile.mobile.api.modules.account.service.AccountService;
import com.mzkj.wanjulaile.mobile.api.modules.account.service.VipSchemeService;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.entity.AccountCoupon;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.entity.Coupon;
import com.mzkj.wanjulaile.mobile.api.modules.coupon.service.AccountCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "auth/accountCoupon")
public class AccountCouponAuthController extends BaseController {

    @Autowired
    private VipSchemeService vipSchemeService;
    @Autowired
    private AccountCouponService accountCouponService;
    @Autowired
    private AccountService accountService;

    /**
     * 根据会员(id,会员类型,优惠券类型)查询优惠券
     * @param vipId  会员类型   可选
     * @param couponType  优惠券类型(1:会员费抵用券  2:运费抵用券) 可选
     * @param  request   accessToken
     * @return
     */
    @RequestMapping(value = "getCoupons")
    @ResponseBody
    public ResultEntity<Map> getCoupons(String vipId, String couponType, HttpServletRequest request){
        ResultEntity<Map> result=new ResultEntity<Map>();
        String accountId=(String)request.getAttribute("accountId");
        Account account=new Account(accountId);
        List<AccountCoupon> accountCouponList;
        //不满足使用条件的优惠券
        List<AccountCoupon> unUseList = Lists.newArrayList();
        //未过期的优惠券
        List<AccountCoupon> couponList = Lists.newArrayList();
        //已过期的优惠券
        List<AccountCoupon> unCouponList = Lists.newArrayList();
        Map map = Maps.newHashMap();
        try {
            if (account != null && !StringUtils.isEmpty(account.getId())) {
                AccountCoupon accountCoupon = new AccountCoupon();
                accountCoupon.setAccount(account);
                if (!StringUtils.isEmpty(couponType)) {
                    Coupon coupon = new Coupon();
                    coupon.setCouponType(couponType);
                    if (!StringUtils.isEmpty(vipId)) {
                        coupon.setVipSchemeType(vipId);
                    }
                    accountCoupon.setCoupon(coupon);
                }

                accountCouponList = accountCouponService.getCouponsByAccountId(accountCoupon);
                //不满足使用条件的优惠券
                unUseList = accountCouponService.getUnUseCoupon(accountCoupon);
                if (!Collections3.isEmpty(accountCouponList)) {
                    for (AccountCoupon accountCoupon1 : accountCouponList) {
                        Date date = new Date();
                        Coupon coupon = accountCoupon1.getCoupon();
                        if (!StringUtils.isEmpty(coupon.getVipSchemeType())) {
                            coupon.setVipSchemeType(coupon.getVipSchemeType().split(",")[0]);
                        }
                        accountCoupon1.setCoupon(coupon);
                        if (accountCoupon1.getExpireDate() == null && "0".equals(accountCoupon1.getDelFlag())) {
                            for (int i = 0; i < accountCoupon1.getCount(); i++) {
                                couponList.add(accountCoupon1);
                            }
                        } else if ("2".equals(accountCoupon1.getDelFlag())) {
                            for (int i = 0; i < accountCoupon1.getCount(); i++) {
                                unCouponList.add(accountCoupon1);
                            }
                        } else if (accountCoupon1.getExpireDate().getTime() > date.getTime() && "0".equals(accountCoupon1.getDelFlag())) {
                            for (int i = 0; i < accountCoupon1.getCount(); i++) {
                                couponList.add(accountCoupon1);
                            }
                        }
                    }
                }
            }
            map.put("unUseList", unUseList);
            map.put("couponList", couponList);
            map.put("unCouponList", unCouponList);
            result.setStatusCode(StatusCode.SC0.getValue());
            result.setMsg(StatusCode.SC0.getDesc());
            result.setData(map);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            result.setStatusCode(StatusCode.SC4002.getValue());
            result.setData(map);
            result.setMsg(StatusCode.SC4002.getDesc());
        }
        return result;
    }

    /**
     * 根据优惠券类型、会员类型,查询会员账号优惠券数量
     * @param
     * @param vipId  会员类型 可选
     * @param couponType  优惠券类型(1:会员费抵用券  2:运费抵用券)
     * @return
     */
    @RequestMapping(value = "getCouponCountByVipType")
    @ResponseBody
    public ResultEntity<Map> getCouponCount(HttpServletRequest request, String vipId, String couponType){
        ResultEntity<Map> result = new ResultEntity<Map>();
        Map<String, Object> map = Maps.newHashMap();
//        AccountCoupon accountCoupon1=new AccountCoupon();
        String accountId=(String)request.getAttribute("accountId");
        Account account=new Account(accountId);
        int couponCount = 0;
        VipScheme vipScheme;
        try{
            AccountCoupon accountCoupon=new AccountCoupon();
            accountCoupon.setAccount(account);
            //查询会员费抵用券数量
            if (account != null && account.getId() != null) {
                Coupon coupon = new Coupon();
                if (!StringUtils.isEmpty(couponType)) {
                    coupon.setCouponType(couponType);
                }
                accountCoupon.setCoupon(coupon);
                if (!StringUtils.isEmpty(vipId)) {
                    coupon.setVipSchemeType(vipId);
                    vipScheme = vipSchemeService.get(vipId);
                    List<AccountCoupon> accountCouponList = accountCouponService.getUseCoupon(accountCoupon);
                    //如果一张优惠券
                    if (!Collections3.isEmpty(accountCouponList) && accountCouponList.size() == 1) {
                        map.put("discountVal", accountCouponService.getCouponVal(accountCouponList.get(0).getId(), vipScheme));
                        map.put("accountCouponId", accountCouponList.get(0).getId());
                    }
                }
                couponCount = accountCouponService.couponCount(accountCoupon);
                map.put("couponCount", couponCount);
            }
            result.setData(map);
            result.setStatusCode(StatusCode.SC0.getValue());
            result.setMsg(StatusCode.SC0.getDesc());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            result.setStatusCode(StatusCode.SC4002.getValue());
            result.setMsg(StatusCode.SC4002.getDesc());
        }
        return result;
    }
}